package com.colorful.exceldemo.helper;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class SpringHelper implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    public SpringHelper() {
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringHelper.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        try {
            return applicationContext.getBean(clazz);
        } catch (Exception var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static <T> T getBean(String name, Class<T> clazz) {
        try {
            return applicationContext.getBean(name, clazz);
        } catch (Exception var3) {
            throw new RuntimeException(var3.getMessage(), var3);
        }
    }

    public static <T> List<T> getBeansOfType(Class<T> clazz) {
        try {
            Map<String, T> map = applicationContext.getBeansOfType(clazz);
            return new ArrayList(map.values());
        } catch (Exception var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static Map<String, Object> getBeanWithAnnotation(Class<? extends Annotation> annotation) {
        try {
            return applicationContext.getBeansWithAnnotation(annotation);
        } catch (Exception var2) {
            throw new RuntimeException(var2.getMessage(), var2);
        }
    }

    public static Environment getEnvironment() {
        return (Environment)getBean(Environment.class);
    }
}